package com.hu.gulimallproduct.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hu.gulimallcommon.utils.PageUtils;
import com.hu.gulimallcommon.utils.Query;
import com.hu.gulimallproduct.project.dao.PmsBrandDao;
import com.hu.gulimallproduct.project.dao.PmsCategoryBrandRelationDao;
import com.hu.gulimallproduct.project.dao.PmsCategoryDao;
import com.hu.gulimallproduct.project.entity.PmsBrandEntity;
import com.hu.gulimallproduct.project.entity.PmsCategoryBrandRelationEntity;
import com.hu.gulimallproduct.project.entity.PmsCategoryEntity;
import com.hu.gulimallproduct.project.service.PmsCategoryBrandRelationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;



@Service()
public class PmsCategoryBrandRelationServiceImpl extends ServiceImpl<PmsCategoryBrandRelationDao, PmsCategoryBrandRelationEntity> implements PmsCategoryBrandRelationService {
    @Autowired
    private PmsCategoryDao categoryDao;
    @Autowired
    private PmsBrandDao brandDao;
    @Autowired
    private PmsCategoryBrandRelationDao relationDao;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<PmsCategoryBrandRelationEntity> page = this.page(
                new Query<PmsCategoryBrandRelationEntity>().getPage(params),
                new QueryWrapper<PmsCategoryBrandRelationEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void saveDetil(PmsCategoryBrandRelationEntity pmsCategoryBrandRelation) {
        Long brandId = pmsCategoryBrandRelation.getBrandId();
        Long catelogId = pmsCategoryBrandRelation.getCatelogId();

        PmsCategoryEntity pmsCategoryEntity = categoryDao.selectById(catelogId);
        PmsBrandEntity pmsBrandEntity = brandDao.selectById(brandId);
        pmsCategoryBrandRelation.setBrandName(pmsBrandEntity.getName());
        pmsCategoryBrandRelation.setCatelogName(pmsCategoryEntity.getName());
        baseMapper.insert(pmsCategoryBrandRelation);
    }

    @Override
    public void updateCategory(Long catId, String name) {
        PmsCategoryBrandRelationEntity categoryBrandRelation = new PmsCategoryBrandRelationEntity();
        categoryBrandRelation.setCatelogId(catId);
        categoryBrandRelation.setCatelogName(name);
        this.update(categoryBrandRelation,new UpdateWrapper<PmsCategoryBrandRelationEntity>().eq("catelog_id",catId));
    }

    @Override
    public void updateBrand(Long brandId, String name) {
        PmsCategoryBrandRelationEntity categoryBrandRelation = new PmsCategoryBrandRelationEntity();
        categoryBrandRelation.setBrandId(brandId);
        categoryBrandRelation.setBrandName(name);
        this.update(categoryBrandRelation,new UpdateWrapper<PmsCategoryBrandRelationEntity>().eq("brand_id",brandId));
    }

    @Override
    public List<PmsBrandEntity> relationBrandsList(Long catId) {
        List<PmsCategoryBrandRelationEntity> list = baseMapper.selectList(new QueryWrapper<PmsCategoryBrandRelationEntity>().eq("catelog_id", catId));
        List<PmsBrandEntity> collect = list.stream().map(item -> {
            Long brandId = item.getBrandId();
            PmsBrandEntity pmsBrandEntity = brandDao.selectById(brandId);
            return pmsBrandEntity;
        }).collect(Collectors.toList());
        return collect;
    }

}